<?php
/**
 * 本地仓库
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/10
 * Time: 13:49
 */
namespace App\Services\Warehouse;
use App\Repositories\Warehouse\WarehouseRepository;
use App\Services\Warehouse\WarehouseService;
use App\Services\Warehouse\AvailableQueryService;
use Illuminate\Support\Facades\DB;

class WarehouseLocalService extends PublicService{

    protected $WarehouseRepository;
    protected $WarehouseService;
    protected $AvailableQueryService;
    public function __construct(WarehouseRepository $warehouseRepository,WarehouseService $warehouseService,AvailableQueryService $availableQueryService)
    {
        parent::__construct('',false);
        $this->WarehouseRepository=$warehouseRepository;
        $this->WarehouseService=$warehouseService;
        $this->AvailableQueryService=$availableQueryService;
    }

    /**
     *  根据WID获取当前仓库基本信息
    */
    public function GetWarehouseInfo($wid){
         $info=$this->WarehouseRepository->GetWarehouseInfo($wid);
         if($info['k3_stockid']){
             $k3info=$this->WarehouseService->ListWarehouse($info['k3_stockid']);
             if($k3info){
                 $info['k3_stockname']=$k3info[0]['FName'];
                 $info['k3_FDocumentStatus']=$k3info[0]['FDocumentStatus'];
                 return $info;
             }else{
                 return $info;
             }
         }else{
             return $info;
         }
    }


    /**
    *   参数说明
     * $goods_sn 商品货号
     * $wid   食得鲜仓库ID
     * 返回的字段说明
     *  FMaterialId   商品ID
     *  FMaterialId.FNumber  商品货号
     *  FMaterialName  商品名称
     *  FStockId  仓库ID
     *  FStockId.FNumber  仓库编号
     *  FStockName  仓库名称
     *  FBaseQty   库存量
     *  FBaseAVBQty  可用量
     *  FBaseAwaitQty  占用量
     */
    public function GetGoodsInventory($goods_sn,$wid){
        if(empty($goods_sn) || empty($wid)){
            return false;
        }
        $k3_number=$this->CheckWarehouseBindingExist($wid);
        if($k3_number){
            $goods_data=$this->AvailableQueryService->GetGoodsAvailableQuery($goods_sn,$k3_number);
            if($goods_data){
                return $goods_data[0];
            }else{
                return false;
            }
        }else{
            return false;
        }

    }


    /**
    *   检查金蝶系统商品物料可用量是否充足
     *  参数说明
     *  $goods_sn  商品货号
     *  $wid  食得鲜平台仓库ID
     *  $quantity 保底数  默认数1
     *  返回
     *  FBaseAVBQty  当前商品物料可用量
     */
    public function CheckGoodsInventoryAVBQty($goods_sn,$wid,$quantity=1){
        $goods_inventory_info=$this->GetGoodsInventory($goods_sn,$wid);
        if($goods_inventory_info[0]['FBaseAVBQty']>=$quantity){
            return $goods_inventory_info[0]['FBaseAVBQty'];
        }else{
            return false;
        }

    }

    /**
    *   检查食得鲜仓库是否绑定对应金蝶仓，若绑定则返回对应绑定的金蝶仓编号
     */
    public function CheckWarehouseBindingExist($wid){
       if(empty($wid)){
           return false;
       }
        $info=$this->WarehouseRepository->GetWarehouseInfo($wid);
       if(!empty($info['k3_number'])){
            return $info['k3_number'];
        }else{
            return false;
        }
    }

    public function CheckGoodsUsableWarehouse(array $goods,$wid){
        if(count($goods)<=0){
            return false;
        }
        $FStockNumber=$this->CheckWarehouseBindingExist($wid);
        if(empty($FStockNumber)){
            $FStockNumber=false;
        }
        $return=$this->AvailableQueryService->GetGoodsWarehouseFSTOCKNUMBER($goods,$FStockNumber);
        if($return){
            $goods=array();
            foreach ($return as $k=>$v){
                $goods[$v->FMATERIALNUMBER]=floor($v->FAVAILABLEQTY); //可发量
            }
            return $goods;
        }else{
            return false;
        }
    }

}
